﻿(function () {
    $(function () {
        var _$dataTable = $('#DataTable');
        var _dataService = abp.services.app.course; //对应application中接口前缀
        //对应AppPermissions里面的Pages_BasicDataModule_Classrooms_Create等变量
        var _permissions = {
            create: abp.auth.hasPermission('Pages.EduAdminModule.Courses.Create'),
            edit: abp.auth.hasPermission('Pages.EduAdminModule.Courses.Edit'),
            'delete': abp.auth.hasPermission('Pages.EduAdminModule.Courses.Delete')
        };
        //新增/编辑窗体定义
        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'EduAdminModule/Courses/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/EduAdminModule/Views/Courses/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        //课程进度表
        var _CourseSchedulesModal = new app.ModalManager({
            viewUrl: abp.appPath + 'EduAdminModule/Courses/CourseSchedulesModal',
            scriptUrl: abp.appPath + 'Areas/EduAdminModule/Views/Courses/_CourseSchedulesModal.js',
            modalClass: 'CourseSchedulesModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });

        //表格定义，包括样式，触发事件等。
        _$dataTable.jtable({

            //title: app.localize('Pages_BasicDataModule_Classrooms'),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getObjects
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '20%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    deleteObject(data.record);
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-clock-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _CourseSchedulesModal({ id: data.record.id });
                                });
                        }

                        return $span;
                    }
                },
                //网格里面字段必须驼峰写法
                courseName: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_CourseName'),
                    width: '20%'
                },
                price: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_Price'),
                    width: '10%'
                },

                grade: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_Grade'),
                    width: '10%'
                },
                subject: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_Subject'),
                    width: '10%'
                },
                subjectType: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_SubjectType'),
                    width: '10%'
                },
                courseType: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_CourseType'),
                    width: '10%'
                },
                isDisable: {
                    title: app.localize('Page_EduAdminModule_Courses_GridColumn_IsDisable'),
                    width: '10%'
                }
            }

        });//表格定义结束

        //加载数据方法
        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter: $('#DataTableFilter').val()
                });
            }
        }
        //删除教室信息
        function deleteObject(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.courseName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _dataService.deleteObject({
                            id: o.id
                        }).done(function () {
                            getObjects(true);
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }
        //打开新增/编辑页面
        $('#CreateNewButton').click(function (e) {
            e.preventDefault();
            _createOrEditModal.open();
        });
        //新增/编辑保存之后回到主页面，重新加载主页面数据。
        abp.event.on('app.CreateOrEditModalSaved', function (result) {
            //alert('return value:' + result.classroomName);
            getObjects(true);
        });

        //加载数据
        getObjects();

        //光标定位到查询条件输入框
        $('#DataTableFilter').focus();
    });
})();